package org.example.com.editor.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.com.editor.workspace.WorkspaceState;

import java.io.File;
import java.io.IOException;


public class JacksonWorkspaceSerializer implements WorkspaceSerializer {
    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void serialize(WorkspaceState memento, File stateFile) throws IOException {
        if (!stateFile.getParentFile().exists()) {
            stateFile.getParentFile().mkdirs();
        }
        objectMapper.writeValue(stateFile, memento);
    }

    @Override
    public WorkspaceState deserialize(File stateFile) throws IOException {
        if (!stateFile.exists()) {
            return null;
        }
        return objectMapper.readValue(stateFile, WorkspaceState.class);
    }
}